package com.inventory.api.controller;


import com.inventory.api.dao.StockCheckOrderDao;
import com.inventory.api.dao.StockCheckTransactionDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by lihu on 2018/2/3.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/inventory", produces = "application/json;charset=utf-8")
public class CheckOrderController {


    @Autowired
    StockCheckOrderDao stockCheckOrderDao;


    @Autowired
    StockCheckTransactionDao checkTransactionDao;


    /**
     * 查询盘点出入库记录
     *
     * @param storeId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/check-record")
    public ResponseEntity getStockRecord(@RequestParam(value = "storeId") String storeId,
                                         @PageableDefault(value = 10, sort = {"operTime"}, direction = Sort.Direction.DESC) Pageable pageable) {
        try {
            return new ResponseEntity(stockCheckOrderDao.findByStoreId(storeId, pageable), HttpStatus.OK);
        } catch (Exception ex) {
            log.error("getStockRecord is error==> storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 获取盘点详情接口
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/check-record-details")
    public ResponseEntity getStockRecordDetails(@RequestParam(value = "id") String id) {

        try {
            return new ResponseEntity(checkTransactionDao.queryCheckOrderId(id), HttpStatus.OK);

        } catch (Exception ex) {
            log.error("getStockRecordDetails is error==> id:{}", id, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);

        }
    }
}
